package com.translation.assistant.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hms.mlsdk.model.download.MLLocalModelManager;
import com.huawei.hms.mlsdk.model.download.MLRemoteModel;
import com.huawei.hms.mlsdk.translate.local.MLLocalTranslatorModel;

import java.util.ArrayList;
import java.util.Set;

public class NetworkUtil {

    //网络状态
    public final static int NETWORL_NONE = 1;
    public final static int NETWORK_WIFI = 2;
    public final static int NETWORK_MOBILE = 3;

    public static int checkNetwork(Context context) {

        ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            NetworkInfo networkInfo = connectMgr.getActiveNetworkInfo();
            if (networkInfo == null || !networkInfo.isAvailable() || !networkInfo.isConnected()) {
                return NETWORL_NONE;
            }

            int networkType = networkInfo.getType();
            if (networkType == ConnectivityManager.TYPE_WIFI) {
                return NETWORK_WIFI;
            }

            if (networkType == ConnectivityManager.TYPE_MOBILE) {
                return NETWORK_MOBILE;
            }
        } catch (Exception e) {
            return NETWORL_NONE;
        }

        // Others: no network by default
        return NETWORL_NONE;
    }

    /*public static MLLocalModelManager manager;
    public static ArrayList<String> downloadedModelList = new ArrayList<>();

    public static void updateModel() {
        if(manager == null){
            manager = MLLocalModelManager.getInstance();
        }
        manager.getModels(MLLocalTranslatorModel.class).addOnSuccessListener(new OnSuccessListener<Set<MLLocalTranslatorModel>>() {
            @Override
            public void onSuccess(Set<MLLocalTranslatorModel> mlLocalTranslatorModels) {

                downloadedModelList.clear();
                //downloadedModelList.add("en");//英文不需要下载，已经存在sdk里面
                for (MLRemoteModel m : mlLocalTranslatorModels) {
                    String modelName = m.getModelName().substring(m.getModelName().indexOf("_") + 1);
                    if (!downloadedModelList.contains(modelName)) {
                        downloadedModelList.add(modelName);
                    }
                }
            }
        });
    }*/
}
